package com.example.myapplication.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.myapplication.models.News;
import com.example.myapplication.R;

import java.util.List;

public class NewsAdapter extends BaseAdapter {
    private Context context;
    private List<News> newsList;
    private LayoutInflater inflater;

    public NewsAdapter(Context context, List<News> newsList) {
        this.context = context;
        this.newsList = newsList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_news, parent, false);
            holder = new ViewHolder();
            holder.newsImage = convertView.findViewById(R.id.news_image);
            holder.newsTitle = convertView.findViewById(R.id.news_title);
            holder.newsDescription = convertView.findViewById(R.id.news_description);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        News news = newsList.get(position);
        holder.newsImage.setImageResource(news.getImageResId());
        holder.newsTitle.setText(news.getTitle());
        holder.newsDescription.setText(news.getDescription());

        return convertView;
    }

    private static class ViewHolder {
        ImageView newsImage;
        TextView newsTitle;
        TextView newsDescription;
    }
}